function quickSort(nums){

    function loop(arr){
       let len = arr.length
       if(len < 2) return arr
       let baseIndex = Math.floor(len/2)
       let baseVal = arr[baseIndex]
       for (let i = 0; i < baseIndex; i++) {
            if(arr[i] > baseVal ){
                let temp = arr[i]
                arr.splice(i, 1)
                arr.splice(baseIndex, 0, temp) 
            }
        }
        console.log(baseVal, arr)
        for (let i = len - 1; i > baseIndex; i--) {
            console.log(arr[i], baseVal)
            if(arr[i] < baseVal){
                let temp = arr[i]
                arr.splice(i, 1)
                arr.splice(baseIndex - 1, 0, temp)
                i += 1
            }
            console.log(arr)
           
       }
       console.log(baseVal, arr)
       return [...loop(arr.slice(0, baseIndex)), baseVal, ...loop(arr.slice(baseIndex + 1, len))]
    }

    return loop(nums)

}


const nums = [2,1,6,7,8,3,5,4]
console.log(nums)
console.log(quickSort(nums))